﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SQLite;

namespace MOJsdk
{
    public class MojSystemVars : SQLiteConnection
    {
        public MojSystemVars(string AppLocal, string DataName)
            : base(Path.Combine(AppLocal, DataName + ".db"))
        {

        }

        public bool Exist<T>(string VarKey)
        {
            CreateTable<SystemVariable<T>>();

            return Table<SystemVariable<T>>().Contains(new SystemVariable<T>(VarKey));
        }
        public void Set<T>(string VarKey, T VarValue)
        {
            SystemVariable<T> var = new SystemVariable<T>(VarKey, VarValue);

            if (Exist<T>(VarKey))
                Update(var);
            else
                Insert(var);
        }
        public T Get<T>(string VarKey)
        {
            if (Exist<T>(VarKey))
                return Table<SystemVariable<T>>().First(z => z.VarKey == VarKey).VarValue;

            throw new Exception("Var not stored");
        }

        /// <summary>
        /// If varkey doesnt exist create one with VarValue as Value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="VarKey"></param>
        /// <param name="VarValue"></param>
        /// <returns>True if varKey is created</returns>
        public bool Create<T>(string VarKey, T VarValue)
        {
            if (Exist<T>(VarKey))
                return false;

            Set<T>(VarKey, VarValue);

            return true;
        }
    }
    public class MojDynamicVars
    {
        Dictionary<string, object> _vars;

        public MojDynamicVars()
        {
            _vars = new Dictionary<string, object>();
        }

        public bool Exist(string VarKey)
        { return _vars.ContainsKey(VarKey); }
        public bool Remove(string VarKey)
        {
            if (!_vars.ContainsKey(VarKey)) return false;

            _vars.Remove(VarKey);

            return true;
        }
        public void Set(string VarKey, object VarValue)
        {
            if (Exist(VarKey))
                _vars[VarKey] = VarValue;
            else
                _vars.Add(VarKey, VarValue);
        }
        public object Get(string VarKey)
        {
            if (Exist(VarKey))
                return _vars[VarKey];

            throw new Exception("Invalida VarKey");
        }
        public T Get<T>(string VarKey)
        {
            object var = Get(VarKey);

            try
            {
                return (T)var;
            }
            catch
            {
                throw new Exception("Invalid VarType");
            }
        }

        public List<string> GetKeys()
        {
            return new List<string>(_vars.Keys);
        }
    }

    public class SystemVariable<T> : IEquatable<SystemVariable<T>>
    {
        public SystemVariable(string Key)
        { VarKey = Key; }
        public SystemVariable(string Key, T Value)
        { 
            VarKey = Key;
            VarValue = Value;
        }
        public SystemVariable() { }

        [PrimaryKey]
        public string VarKey
        { get; set; }
        public T VarValue
        { get; set; }

        public bool Equals(SystemVariable<T> Var)
        {
            return VarKey == Var.VarKey;
        }
    }
}
